<template>
    <div class="">
    <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> app版本列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
			 <el-button type="primary" round @click="handleEdit(0,0)"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增APP版本</el-button>
			<div style="height:40px"></div>
			<el-form>		
    			<el-form-item label="app平台:">
                	<el-select v-model="platform" placeholder="性别" >
					    <el-option
					      v-for="item in options"
					      :key="item.value"
					      :label="item.label"
					      :value="item.value">
					    </el-option>
					   </el-select>
					  <el-button type="primary" style="margin-left:20px;" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button>
					</el-form-item> 
			</el-form>		
            <div style="height:40px"></div>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="id" label="版本ID" width="100" align="center">
                    </el-table-column>
                    <el-table-column prop="version_num" label="版本号" width="120" align="center">
                    </el-table-column>
                    
					 <el-table-column prop="platformTxt" label="平台" width="80" align="center">
                    </el-table-column>
					<el-table-column prop="upgrade_url" label="下载地址"  width="800" align="center">
                    </el-table-column>
					<el-table-column type="expand" label="查看版本描述" width="120" align="center">
						<template slot-scope="props">
							<div class="bs-callout bs-callout-danger">
								<span style="padding-left:30px;"></span>{{props.row.upgrade_desc}}
							</div>
						</template>
                   </el-table-column>
					 <el-table-column prop="create_time" label="发布时间" width="200" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center" >
                        <template slot-scope="scope" >
                            <el-button class="edit" size="small" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
              
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
         <!-- 新增编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="isAddAccout"  center >
            <el-form style="width:800px; margin:0 auto"  label-width="110px">
                <el-form-item label="平台类型：">
                        <el-select v-model="type" placeholder="请选择"> 
                            <el-option v-for="item in typeOptions" :key="item.url" :label="item.label" :value="item.url">
                            </el-option>
                        </el-select>
                </el-form-item> 
                <el-form-item label="APP版本号：">
                    <el-input style="width:300px;" type="number" v-model="num" auto-complete="off"  placeholder="请输入APP版本号" ></el-input>
                </el-form-item>
				<el-form-item label="APP下载地址：">
                    <el-input style="width:300px;" v-model="url" auto-complete="off"  placeholder="请输入APP下载地址" ></el-input>
                    <p><span style="color:red;">安卓版下载地址为：http://sj.qq.com/myapp/detail.htm?apkName=com.meijiang.printsearch</span></p>
                    <p><span style="color:red;">Ios版下载地址为： https://itunes.apple.com/cn/app/%E5%8D%B0%E5%88%B7%E6%90%9C%E6%90%9C/id1415458460?mt=8</span></p>
                </el-form-item>
					<el-form-item label="APP版本描述：">
                    	<el-input style="width:300px;" type="textarea" v-model="txt"></el-input>
                	</el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="onCancel">取 消</el-button>
                <el-button type="primary" @click="onCommit">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除提示框 -->


    </div>
</template>
<script>
import {getVersionList,postAddVersion,postDeleteVersion,postEditVersion} from '@/api/appVersion'
export default {
    name:"appList",
    data() {
        return {
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            totalElements:1,//默认总条数
            totalPages:1,//默认总页数
            tableData:[],//表格数据
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
			delVisible:false, //默认dialog 不弹出（删除提示）
			isAddAccout:false,//编辑或新增界面
			id:'',//当前对象id
            options: [{
			          value: '0',
			          label: '全部'
			        }, {
			          value: '1',
			          label: '安卓'
			        }, {
			          value: '2',
			          label: 'IOS'
			        }],
			platform:'0',//app平台类型：0不限平台、1安卓、2IOS
			typeOptions:[{url:1,label:"安卓"},{url:2,label:"IOS"},],//平台类型：1安卓、2IOS
			type:'',//平台类型
			num:'',//app版本号
			txt:'',//app版本描述
			url:'',//app下载地址
			title:'',//编辑或新增弹出界面标题
		}
    },
    //实例创建
    created(){
        //
        this.getList();
    },
    //计算属性
    computed:{
    	
    },
    //方法
    methods: {
		//获取app版本列表
        getList(){
        	var param = {
        		platform:this.platform,
				pageIndex:this.pageIndex,
				pageSize:this.pageSize
        	}
        	getVersionList(param).then((response) => {
				// console.log(response);
                if(response.code==200){
					var data = response.data;//当前返回的数据
                    this.totalElements = data.total; //总条数
                    this.totalPages = Math.ceil(data.total/this.pageSize);//总页数
					console.log(data);
					let data = data.list;//具体的列表数据
					let newAry = [];
					data.forEach(element => {
						let obj = {};//接收每一个数据
						obj.create_time = element.create_time;
						obj.id = element.id;
						obj.upgrade_desc = element.upgrade_desc;
						obj.state = element.state;
						obj.version_num = element.version_num;
                        obj.upgrade_url = element.upgrade_url;
                        obj.platform = element.platform;
						if(element.platform == 1){//安卓平台
							obj.platformTxt = "安卓";
						}
						if(element.platform == 2){//ios平台
							obj.platformTxt = "IOS";
						}
						newAry.push(obj);
					});
                   this.tableData = newAry;//列表数据
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
        	})
        },
		//编辑或新增 显示弹出界面
        handleEdit(index,row){
            console.log(row);
            if(row.id && row.id>0){//编辑状态
                this.id = row.id;
                this.title = '修改APP版本信息'
                this.type = row.platform;
                this.num = row.version_num;
                this.url = row.upgrade_url;
                this.txt = row.upgrade_desc;
                this.dialogState = true;
            }else{//新增状态
                this.title = '新增APP版本信息'
                //每次新增清空原有数据 
                this.type = '';
                this.num = '';
                this.url = '';
                this.txt = '';
                this.dialogState = false;
            }
            this.isAddAccout = true;
		},
		 //取消新增或编辑操作
        onCancel(){
            this.isAddAccout = false;
        },
        //确定新增或编辑
        onCommit(){
			if(this.type==""){
                this.$notify.info({title: '提示',message: '请先选择平台类型'});
                return false;
			}
			if(this.num==""){
                this.$notify.info({title: '提示',message: '请先输入APP版本号'});
                return false;
            }
            if(this.url == ""){
                this.$notify.info({title: '提示',message: '请先输入APP下载地址'});
                return false;
			}
			if(this.txt == ""){
                this.$notify.info({title: '提示',message: '请先输入APP版本描述'});
                return false;
			}else{
				let param ={}
                param.type = this.type;
                param.num = this.num;
                param.url = this.url;
				param.txt = this.txt;
				
				if(this.dialogState){//编辑
                    param.id = this.id;
                    this.subEdit(param);  
                }else{//新增
                    this.subAdd(param);  
                }
			}
			
		},
		//提交新增
        subAdd(param){
            console.log(param);
            let _this = this ;
            postAddVersion(param).then(response=>{
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，新增成功',
                        type: 'success'
                    });
                    this.isAddAccout =false;
                    setTimeout(function(){
                        _this.getList();
                    },1500);

                }else{
                    this.$notify.error({
                        title: '新增失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }
            }).catch(error=>{
                console.log(error);
            });
        },
        //提交编辑
        subEdit(param){
            let _this = this ;
            postEditVersion(param).then(response=>{
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，修改成功',
                        type: 'success'
                    });
                    this.isAddAccout =false;
                    setTimeout(function(){
                        _this.getList();
                    },1500);

                }else{
                    this.$notify.error({
                        title: '修改失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }
            }).catch(error=>{
                console.log(error);
            });
        },
        //删除
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.id && row.id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }else{
                // console.log("没触发");
            }
        },
        // 确定删除
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                console.log(this.delId)
                postDeleteVersion(this.delId).then(response=>{
                    // console.log(response);
                    if(response.code==200){
                        this.tableData.splice(this.delIndex, 1);
                        this.$message.success('删除成功');
                        this.delVisible = false;
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                })
            }
            
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getList();
        },
        //查询
        onSelect(){
            this.pageIndex =1;
            this.getList();
        },
        
    }
}
</script>
<style lang="stylus" scoped>
	.edit{
	    color #ff9800;
	}
	.del{
	    color #e51c23;
	}
	.download{
	    color #67C23A;
	}
.bs-callout {
    padding: 20px;
    border: 1px solid #eee;
    border-left-width: 5px;
    border-radius: 3px;
}
.bs-callout-danger {
    border-left-color: #ce4844;
}
</style>


